#!/bin/bash

version="v1.0.0"
# dockerfile目录
dir=""
# prefix
user="magein"
# service name
service_name=""
# service_tag
service_tag=""
spend_time=1
quite=1

format() {
  printf "%-30s%-10s" "$1" "$2"
  echo ""
}

printError() {
  echo "Error:"
  for item in "$@"; do
    echo -e "   $item"
  done
  exit 1
}

usage() {
  echo "Usage:"
  format " -h,--help" "Get help"
  format " -v,--version" "Get version"
  format " -u,--user" "set username"
  format " -n,--name" "set service name"
  format " -t,--tag" "set service tag"
  format " -d,--dir" "dockerfile directory"
  format " --close" "turn off time spent display"
  echo "Example:"
  echo "/root/scripts/docker-build -d /usr/local/docker/dockerfile/nginx/1.22.1 -n nginx -t 1.22.1"
}

eval set -- "$(getopt -q -o "hvqd:u:n:t:" -l help,version,quite,dir:,user:,name:,tag:,close -- "$@")"

while [ -n "$1" ]; do
  case $1 in
  -h | --help)
    usage
    exit
    ;;
  -v | --version)
    echo "${version}"
    exit
    ;;
  -u | --user)
    user="$2"
    shift 2
    ;;
  -n | --name)
    service_name="$2"
    shift 2
    ;;
  -t | --tag)
    service_tag="$2"
    shift 2
    ;;
  -d | --dir)
    dir="$2"
    shift 2
    ;;
  -q | --quite)
    quite=0
    shift 1
    ;;
  --close)
    spend_time=0
    shift 1
    ;;
  --)
    shift 1
    break
    ;;
  *)
    break
    ;;
  esac
done

if [ ! -d "$dir" ]; then
  printError "Please specify the dockerfile path"
fi

if [ ! -f "$dir/dockerfile" ]; then
  printError "No dockerfile found:$dir"
fi

if [ ${#service_name} -eq 0 ]; then
  printError "Please enter the image name : -n or --name"
fi

if [ ${#service_tag} -eq 0 ]; then
  printError "Please enter the image tag : -t or --tag"
fi

#
# 检查是否已经构建镜像
#
# 0 已经构建 1 尚未构建
#
# 返回0 表示没有错误
#
checkImage() {
  image_name="$user/${service_name}:${service_tag}"
  res=$(sudo docker images | awk '{print $1":"$2}' | grep "$image_name")
  # 尚未构建
  if [[ ${#res} -eq 0 ]]; then
    return 1
  fi
  # 已经构建
  return 0
}

dir=$(echo "$dir" | sed "s/\/\+$//")

files=($(ls "$dir"))

for file in "${files[@]}"; do

  target_file="$dir/${file}"

  if [ ! -f "$file" ]; then
    sudo cp "$target_file" "$file"
  fi
done

showSpendTime() {
  spin='-\|/'
  i=0
  time=0
  while true; do
    building=$(ps -aux | grep "$user/${service_name}:${service_tag}" | grep -v "grep")
    if [ ${#building} -eq 0 ]; then
      docker build -t "$user/${service_name}:${service_tag}" . >/dev/null 2>&1 &
    fi
    i=$(((i + 1) % 4))
    printf "\r%10s    ${spin:$i:1} %5ss" "Build $user/${service_name}:${service_tag}" $time
    s=1
    sleep $s
    time=$(("$time" + 1))
    if [ $i -eq 3 ]; then
      # 已经构建 则结束循环
      if checkImage; then
        break
      fi
    fi
  done
}

if [ $spend_time -eq 1 ]; then
  showSpendTime
else
  docker build -t "$user/${service_name}:${service_tag}" . >/dev/null 2>&1
fi

for item in "${files[@]}"; do
  if [ -f "$item" ]; then
    sudo rm -rf "$item"
  fi
done

if checkImage; then
  echo ""
  echo ""
  echo "Successfully tagged $user/${service_name}:${service_tag}"
  exit 1
else
  echo ""
  echo ""
  echo "Fail tagged $user/${service_name}:${service_tag}"
  exit 0
fi
